package com.lhn.netty.netty_demo.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * 解码器
 *
 * @author lhn
 * @date 2025/06/15 17:46
 * @since 1.0.0
 **/
public class MyMessageDecoder extends ReplayingDecoder <Void> {


    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List <Object> out) throws Exception {
        System.out.println("MyMessageDecoder decode 被调用");
        // 需要将  ByteBuf 转换成 MyMessage数据包
        int length = in.readInt(); // 读取长度
        byte[] bytes = new byte[length];
        in.readBytes(bytes);
        MessageProtocol myMessage = new MessageProtocol();
        myMessage.setLength(length);
        myMessage.setContent(bytes);
        out.add(myMessage);
    }
}